﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleWebsite.Models;

namespace SaleWebsite.Controllers
{
    public class EmployerController : Controller
    {
        //
        // GET: /Employer/

        public ActionResult Index()
        {
            return View(Employee.ConvertAll(Employee.GetAll()));
        }
        /// <summary>
        /// insert new employee
        /// </summary>
        /// <param name="name"></param>
        /// <param name="address"></param>
        /// <param name="phone"></param>
        /// <param name="id_cart"></param>
        /// <param name="email"></param>
        /// <param name="dob"></param>
        /// <returns></returns>
        public ActionResult insert(string name, string address, string phone, string id_cart, string email,string dob,string userName,string passWord)
        {
            
               try
               {
                   Employee item = new Employee() { Name = name, Address = address, Phone = phone, Id_card = id_cart, Email = email, UserName = userName, Password = passWord, DOB = dob, UserType_Name = "Un know" };
                   ulong id = item.Insert();
                   if (id > 0)
                   {
                       item.Id = id;
                       return PartialView("employee_row", item);
                   }else
                   {
                       return Json(new { msg = "insert khong thanh cong" });
                   }
                
               }
               catch (System.Exception ex)
               {
               	   return Json(new {msg=ex.Message});
               }
               
            

           


        }
        public JsonResult delete(ulong id)
        {
            Employee item = new Employee() { Id = id };
            if (item.Delete() > 0)
            {
                return Json(new { status = "success" });
            }
            else
            {
                return Json(new { status = "not" });
            }

        }
        public ActionResult Save_Employee(string name, string address, string phone, string id_cart, string email, string dob,ulong id)
        {
            try
            {
                Employee item = new Employee() { Id = id, Name = name, Address = address, Phone = phone, Id_card = id_cart, Email = email,DOB=dob };
                if (item.Save() > 0)
                {
                    //return Json(new { error = "update Faile" });
                    return PartialView("employee_row", item);
                }
                else
                {
                    return Json(new { error = "update Faile" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }

    }
}
